<template>
  <div class="app-container">
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="440px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="80px">
          <el-form-item prop="officePlace" label="办公场地" size="small">
            <el-input v-model="teamRow.officePlace"></el-input>
          </el-form-item>
          <el-form-item prop="buildingInfo" label="楼层" size="small">
            <el-input v-model="teamRow.buildingInfo"></el-input>
          </el-form-item>
          <el-form-item prop="area" label="面积" size="small">
            <el-input v-model.number="teamRow.area">
              <span slot="suffix">M<sup>2</sup></span>
            </el-input>
          </el-form-item>
          <el-form-item label="图片" size="small">
            <input type="file" ref='imagefile' accept="image/*" @change="fileUpload($event,'imagefile')">
          </el-form-item>
          <el-form-item prop="remark" label="备注说明" size="small">
            <el-input type="textarea" v-model="teamRow.remark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      @openVisible="openVisible"
    >
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.officeId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog title="文件查看" :visible.sync="imgVisible" width="600px">
      <img class="img-dialog" :src="imgMsg.src" alt="">
    </el-dialog>
  </div>
</template>

<script>
  import {
    addOfficePlace,
    deleteOfficePlace,
    getListPlace,
    selectOfficePlace,
    updateOfficePlace
  } from '@/api/officeplace'
  import myTable from '@/components/myTable'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'

  const area = {
    props: ['colConfig'],
    template: `
  <el-table-column :label="colConfig.label" :min-width="colConfig.minWidth">
      <span slot-scope="scope">
        <span slot="suffix">{{scope.row[colConfig.prop]}}M<sup>2</sup></span>
      </span>
  </el-table-column>
  `
  }

  const image = {
    props: ['colConfig'],
    methods: {
      openVisible(label, src) {
        const msg = {
          label: label,
          src: src
        }
        this.$emit('openImgVisible', msg)
      }
    },
    filters: {},
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        <a v-if="scope.row[colConfig.prop]" @click="openVisible(colConfig.label,scope.row[colConfig.prop])">查看</a>
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'officePlace', label: '办公场地', minWidth: '30%', fixed: true },
          { prop: 'buildingInfo', label: '楼层', minWidth: '30%' },
          { prop: 'area', label: '面积', minWidth: '30%', component: area },
          { prop: 'image', label: '图片', width: '150', component: image },
          { prop: 'remark', label: '备注说明', width: '150' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        professions: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增团队表',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        pca: pca,
        rules: {
          officePlace: [
            { required: true, message: '请输入办公场地', trigger: 'change' }
          ],
          buildingInfo: [
            { required: true, message: '请输入楼层', trigger: 'change' }
          ],
          area: [
            { required: true, message: '请输入面积', trigger: 'change' },
            { type: 'number', message: '面积必须为数字值', trigger: 'change' }
          ]
        },
        visible: false,
        imgVisible: false,
        imgMsg: {}
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      teamId() {
        return this.$store.state.user.teamId
      }
    },
    methods: {
      fetchData() {
        const params = {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
        this.listLoading = true
        getListPlace(params).then(response => {
          console.log(response)
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增办公场地'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 1
        this.fileClear()
        this.$set(this.teamRow, 'teamId', this.teamId)
      },
      editTeam(officeId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改办公场地'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 0
        const params = {
          officeId: officeId
        }
        selectOfficePlace(params).then(response => {
          this.teamRow = response.responseBody
        })
        this.fileClear()
      },
      submitForm(formName) {
        const params = new FormData()
        params.append('teamId', this.teamRow.teamId)
        params.append('officePlace', this.teamRow.officePlace)
        params.append('buildingInfo', this.teamRow.buildingInfo)
        params.append('area', this.teamRow.area)
        params.append('imagefile', this.teamRow.imagefile)
        params.append('remark', this.teamRow.remark || '')
        if (!this.dialogType) {
          params.append('officeId', this.teamRow.officeId)
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addOfficePlace(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateOfficePlace(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        console.log(this.$refs[`popover-${scope.$index}`])
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          officeId: scope.row.officeId,
          isDel: 1
        }
        deleteOfficePlace(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      addressChange() {
        this.teamRow.provinceId = this.teamRow.addressId[0]
        this.teamRow.cityId = this.teamRow.addressId[1]
        this.teamRow.address = this.pca[86][this.teamRow.addressId[0]] + this.pca[this.teamRow.addressId[0]][this.teamRow.addressId[1]]
      },
      fileClear() {
        if (this.$refs.imagefile) {
          this.$refs.imagefile.value = ''
        }
      },
      fileUpload(event, scheduleName) {
        this.$set(this.teamRow, scheduleName, event.target.files[0])
      },
      openVisible(msg) {
        this.imgVisible = true
        this.imgMsg = msg
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
    .img-dialog {
      max-width: 540px;
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  a, a:focus, a:hover {
    color: #409EFF;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
